var vm = new Vue({
    el: '#contentContainer',
    data: {
        form: {
            roleName: '',
            roleDesc: '',
            resourceList: []
        },
        rules: {
            roleName: [
                {required: true, message: '请输入角色名', trigger: 'blur'},
                {min: 0, max: 50, message: '长度不能超过50个字符', trigger: 'blur'}
            ],
            roleDesc: [
                {required: true, message: '请输入角色描述', trigger: 'blur'},
                {min: 0, max: 50, message: '长度不能超过100个字符', trigger: 'blur'}
            ]
        },
        resourceTree: [],
        treeProps: {
            children: 'children',
            label: 'text'
        }
    },
    created: function () {
        var $this = this;

        Common.getJson('/sys/role/getResourceTreeList',{},
            function (response) {
                if (!response || response.status != 200)
                    return;

                var data = response.result;

                // 属性配置信息
                var attributes = {
                    // 标识字段名
                    keyField: 'resourceId',
                    // 上级标识字段名
                    parentKeyField: 'parentId',
                    // 文本字段名
                    textField: 'name',
                    // 根节点标识
                    rootParentId:0
                };

                $this.resourceTree = Common.convertTreeData(data, attributes);
            });
    },
    methods: {
        submitForm: function (formName) {
            var $this = this;

            $this.$refs[formName].validate(function (valid) {
                if (!valid)
                    return false;

                $this.form.resourceList=$this.$refs.tree.getCheckedKeys();
                Common.postJson('/sys/role/add', $this.form,
                    function (response) {
                        if (!response)
                            return;

                        //保存成功
                        if (response.status == 200 && response.result.success)
                            window.location.href = '/sys/role/list';
                        else {
                            var errMsg = response.status == 200 ? response.result.errMsg : response.msg;

                            $this.$notify.error({
                                title: '错误',
                                message: errMsg,
                                offset: $("#main",window.parent.document).height()-120,
                                duration: 0
                            });
                        }
                    });
            });
        },
        resetForm: function (formName) {
            this.$refs[formName].resetFields();
        }
    }
});